import { Component, ViewChild, Injector, Output, EventEmitter, ElementRef } from '@angular/core';
import { ModalDirective } from 'ngx-bootstrap';
import { PipelineServiceProxy, PipelineListDto } from '@shared/service-proxies/service-proxies';
import { AppComponentBase } from '@shared/common/app-component-base';

import { finalize } from 'rxjs/operators';


@Component({
  selector: 'actAbnormalModal',
  templateUrl: './act-abnormal-modal.component.html'
})

export class ActAbnormalModalComponent extends AppComponentBase {

  @ViewChild('actionModal') modal: ModalDirective;

  @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

  active = false;
  saving = false;

  pipe: PipelineListDto = new PipelineListDto();

  actAbnormal = '';
  actNames = new Array<string>();

  constructor(
    injector: Injector,
    private _pipelineService: PipelineServiceProxy
  ) {
    super(injector);
  }

  show(pipe: PipelineListDto, actAbnormal: string): void {

    this.actAbnormal = actAbnormal;
    this.pipe = pipe;

    this.active = true;
    this._pipelineService.getAbnormalActIncludeComponents(actAbnormal)
      .subscribe((result) => {
        this.actNames = result.items;
        this.modal.show();

      });
  }

  save(): void {
    this.saving = true;

    this._pipelineService.insertAbnormalComponentByName(this.pipe.id, this.actAbnormal)
      .pipe(finalize(() => {
        this.close();
        this.saving = false;
      }))
      .subscribe((result) => {
        this.modalSave.emit(null);
      });
  }

  close(): void {
    this.saving = false;
    this.modal.hide();
  }
}
